package com.gjy.rocketmq.product;

import com.gjy.rocketmq.RocketUtil;
import com.google.common.collect.Lists;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * 批处理
 * 需要注意的是批量消息的大小不能超过 1MiB（否则需要自行分割），其次同一批 batch 中 topic 必须相同。
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-29 10:12:28
 */
public class BatchProduct {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer(RocketUtil.ProducerGroup);
        producer.setNamesrvAddr(RocketUtil.NAME_SRV);
        producer.start();
        ArrayList<Message> list = Lists.newArrayList();

        for (int i = 0; i < 10; i++) {
            Message message = new Message(RocketUtil.TOPIC_TEST, "HHH".getBytes(StandardCharsets.UTF_8));
            list.add(message);
        }
        SendResult result = producer.send(list);
        System.out.println("result = " + result);

        TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
        producer.shutdown();
    }
}
